package org.component 
{
	import com.adobe.utils.IntUtil;
	import com.boyaa.tools.Utils;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	import org.core.Display;
	
	/**
	 * ...
	 * @author ...
	 */
	
	[Event(name = "change", type = "flash.events.Event")]
	
	public class Stepper extends Display 
	{
		
		private var _btn_reduce:SimpleButton = null;
		
		private var _btn_increase:SimpleButton = null;
		
		private var _txt_count:TextField = null;
		
		private var _value:int = 0;
		
		
		public function Stepper(ui:MovieClip = null) 
		{
			super(ui);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_btn_reduce = assest["btn_reduce"];
			_btn_increase = assest["btn_increase"];
			_txt_count = assest["txt_count"];
		}
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
			_btn_increase.addEventListener(MouseEvent.CLICK, onIncrease);
			_btn_reduce.addEventListener(MouseEvent.CLICK, onReduce);
			_txt_count.addEventListener(Event.CHANGE,onChange)
		}
		
		private function onChange(e:Event):void 
		{
			dispatchEvent(e);
		}
		
		
		
		private function onIncrease(event:MouseEvent):void
		{
			if (max != 0)
			{
				if (value >= max) return;
			}
			value += count;
		}
		private function onReduce(event:MouseEvent):void
		{
			if (value <= 0) return;
			value-= count;
		}
	
		
		public function get value():int { return parseInt(_txt_count.text); }
		public function set value(v:int):void 
		{ 
			_value = v;
			
			_txt_count.text = _value.toString();
			
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		private var _count:int = 0;
		public function get count():int { return _count }
		public function set count(value:int):void { _count = value; }
		
		private var _max:int = 0;
		public function get max():int { return _max; }
		public function set max(value:int):void { _max = value; }
		
		public function setDisable(value:Boolean):void
		{
				_btn_reduce.mouseEnabled = value;
				if (value)
				{
					_btn_reduce.filters = [];
				}
				else
				{
					_btn_reduce.filters = [new ColorMatrixFilter(Utils.getGrayMatrix())];
				}
				
			
		}
		
	}

}